<template>
  <div>
    <h1>用户管理</h1>
    <el-row>
      <el-col>
        <div style="display: flex;justify-content: center">
          <el-select size="small" style="width: 120px;margin-right: 20px;" value="username" placeholder="请选择">
            <el-option key="0" label="用户名" value="username"></el-option>
          </el-select>
          <el-input size="small" v-model="username" style="width: 220px;margin-right: 20px;"></el-input>
          <el-button size="small" type="primary" @click="users()">搜索</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="usersList"
          border
          style="width: 100%">
          <el-table-column prop="id" label="用户id" align="center" width="80"></el-table-column>
          <el-table-column prop="username" label="用户名" align="center" width="100"></el-table-column>
          <el-table-column prop="status" label="状态" width="100" :formatter="versionFormatter"></el-table-column>
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <el-table-column prop="bir" label="生日"></el-table-column>
          <el-table-column prop="image" width="100" align="center" label="头像">
            <template slot-scope="scope">
              <el-image :src="scope.row.image" style="width: 80px; height: 80px" fit="contain"></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="lastLoginTime" label="最后登录时间"></el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleDetail(scope.row.id)">编辑</el-button>
              <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页-->
        <el-row class="f-jc pagination">
            <el-pagination
              background
              :page-size="3"
              layout="prev, pager, next"
              @current-change="changePage"
              :total="total">
            </el-pagination>
        </el-row>
      </el-col>
    </el-row>

    <!-- 修改用户信息-->
    <el-row>
      <el-col :span="24">
        <el-dialog title="修改用户信息" center width="30%" :visible.sync="userDialogFormVisible">
          <el-form :model="user">
            <el-form-item label="用户名" :label-width="formLabelWidth">
              <el-input size="small" v-model="user.username" style="width: 300px" disabled></el-input>
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth">
              <el-input size="small" v-model="user.phone" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth">
              <el-select size="small" v-model="user.status" style="width: 300px">
                <el-option label="激活" value="1"></el-option>
                <el-option label="锁定" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="userDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="editUser">确 定</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { deleteUser, editUser, getUser, users } from '@/api/user'

export default {
  name: 'Index',
  data() {
    return {
      formLabelWidth: '120px',
      userDialogFormVisible: false,
      usersList: [],
      user: {},
      username: '',
      // 对象的总条数
      total: 0,
      // 默认页码 获取是第一页的值
      page: 1
    }
  },
  created() {
    // 用户列表
    this.users();
  },
  methods: {
    // 改变页码
    changePage(page) {
      console.log(page);
      this.page = page;
      // 每当页码改变时 重新发起请求获取当前页的对象
      this.users();
    },
    users() {
      // 获取页码
      const params = { page: this.page };
      if (this.username) {
        params.name = this.username;
      }
      // 调用请求方法  获取后端的数据
      users(params).then(res => {
        this.usersList = res.data.data.users;
        this.total = res.data.data.total;
      }).catch(err => err);
    },
    // 处理删除
    handleClick(row) {
      if (this.usersList.length === 1) this.page = this.page - 1 && 1
      // this.usersList.length === 1 && (this.page = this.page - 1 && 1)
      deleteUser(row.id).then(res => {
        this.users()
      }).catch(err => err);
    },
    // 处理修改
    handleDetail(id) {
      this.userDialogFormVisible = true;
      getUser(id).then(res => {
        this.user = res.data.data;
      }).catch(err => err);
    },
    editUser() {
      editUser(this.user).then(res => {
        this.user = {};
        this.userDialogFormVisible = false;
        this.users();
      }).catch(err => err);
    },
    versionFormatter(row, column, cellValue, index) {
      if (row.status === 1) {
        return '已激活';
      }
      if (row.status === 0) {
        return '未激活'
      }
      if (row.status === 2) {
        return '已锁定'
      }
      return '不可用';
    }

  }
}
</script>

<style scoped>
.el-row {
  margin-bottom: 10px;
}
.pagination{
  margin-top: 30px;
}
</style>
